<template>
  <div>
    <PageTools>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="人员搜索">
          <el-input
            v-model="formInline.userName"
            placeholder="请输入"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="Search"
          >查询</el-button>
        </el-form-item>
      </el-form>
    </PageTools>
    <el-card class="main">
      <el-button
        style="margin-bottom: 10px"
        type="primary"
        size="small"
        icon="el-icon-circle-plus-outline"
        @click="addForm"
      >新建</el-button>
      <!-- 表格 -->
      <el-table
        v-loading="loading"
        :data="list"
        style="width: 100%"
        :header-cell-style="{
          fontWeight: 'normal',
          background: '#f3f6fb',
        }"
      >
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column label="人员名称" prop="userName" />
        <el-table-column label="归属区域" prop="regionName" />
        <el-table-column label="角色" prop="roleId">
          <template #default="{ row }">
            {{ roleList[+row.roleId] }}
          </template>
        </el-table-column>
        <el-table-column label="联系电话" prop="mobile" />
        <el-table-column label="创建日期" prop="updateTime" width="170" />
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              size="small"
              @click="detailsFn(row)"
            >修改</el-button>
            <el-button type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row
        v-if="totalPage !== 1"
        type="flex"
        justify="space-between"
        align="middle"
      >
        <el-col :span="19">
          <p class="paginationHeader">
            {{ `共${totalCount}条记录 第${page.pageIndex}/${totalPage}页` }}
          </p>
        </el-col>
        <el-col :span="5">
          <el-pagination
            background
            :current-page="page.pageIndex"
            :page-size="page.pageSize"
            :total="totalCount"
            layout="prev, next"
            prev-text="上一页"
            next-text="下一页"
            @current-change="changePage"
          />
        </el-col>
      </el-row>
    </el-card>
    <!--新增设备类型-->
    <el-dialog
      :title="title"
      :visible="dialogUser"
      width="45%"
      @open="opneDialog"
      @close="closeDialog"
    >
      <el-form
        ref="userRef"
        label-width="120px"
        :model="userForm"
        :rules="userFormRules"
      >
        <el-form-item label="人员名称" prop="userName">
          <el-input
            v-model="userForm.userName"
            type="text"
            placeholder="请输入内容"
            maxlength="5"
            show-word-limit
            style="width: 80%"
          />
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select
            v-model="userForm.roleId"
            style="width: 80%"
            placeholder="请选择"
          >
            <el-option
              v-for="item in roleIdList"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="mobile">
          <el-input
            v-model="userForm.mobile"
            type="text"
            placeholder="请输入"
            maxlength="11"
            show-word-limit
            style="width: 80%"
          />
        </el-form-item>
        <el-form-item label="负责区域" prop="regionId">
          <el-select
            v-model="userForm.regionName"
            style="width: 80%"
            placeholder="请选择"
            @change="changeRegion"
          >
            <el-option
              v-for="item in regionIdList"
              :key="item.id"
              :label="item.name"
              :value="`${item.id}|${item.name}`"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="头像" prop="image">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :http-request="upload"
            :before-upload="beforeAvatarUpload"
          >
            <!-- 上传后展示的图片 -->
            <img v-if="userForm.image" :src="userForm.image" class="avatar">
            <!-- 默认的加号图片 -->
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
          <span> {{ `支持扩展名:jpg、png,文件不得大于100kb` }}</span>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-checkbox v-model="userForm.status">是否启用</el-checkbox>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancel" @click="closeDialog">取 消</el-button>
        <el-button
          type="primary"
          class="sure"
          @click="userConfig"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { Regionsearch } from '@/api/node'
import { userList, roleList, addroleList, editroleList } from '@/api/userIndex'
export default {
  data() {
    return {
      formInline: { userName: '' },
      loading: false,
      list: [],
      page: {
        pageIndex: 1,
        pageSize: 10
      },
      pageList: {
        pageIndex: 1,
        pageSize: 100000
      },
      totalCount: 0,
      totalPage: 0,
      roleList: {
        2: '运营人员',
        3: '维修员'
      },
      dialogUser: false,
      roleIdList: [],
      regionIdList: [],
      userForm: {
        userName: '', // 用户名
        roleId: '', // 角色id
        mobile: '', // 手机号
        regionId: '', // 所属区域id
        regionName: '', // 所属区域名
        status: '', // 启用状态
        image: '' // 图片
      },
      userFormRules: {
        userName: [{ required: true, message: '请输入', trigger: 'change' }], // 用户名
        roleId: [{ required: true, message: '请输入', trigger: 'change' }], // 角色id
        mobile: [
          { required: true, message: '请输入', trigger: 'blur' },
          {
            pattern: /^((0\d{2,3}-\d{7,8})|(1[34578]\d{9}))$/,
            message: '请输入正确号码',
            trigger: 'blur'
          }
        ], // 手机号
        regionId: [{ required: true, message: '请输入', trigger: 'change' }], // 所属区域id
        regionName: [{ required: true, message: '请输入', trigger: 'change' }], // 所属区域名
        status: [{ required: true, message: '请输入', trigger: 'change' }], // 启用状态
        image: [{ required: true, message: '请输入', trigger: 'change' }] // 图片
      }
    }
  },
  computed: {
    title() {
      return this.userForm.id ? '编辑人员' : '新增人员'
    }

  },
  created() {
    this.userList()
  },
  methods: {
    // 表格
    async userList(className) {
      this.loading = true
      const { data } = await userList({ ...this.page, ...className })
      this.loading = false
      this.list = data.currentPageRecords
      this.totalCount = Number(data.totalCount)
      this.totalPage = Number(data.totalPage)
    },
    // 1.查询
    Search() {
      this.userList(this.formInline)
    },
    // 2.新增
    addForm() {
      this.dialogUser = true
    },
    // 3.分页
    changePage(val) {
      this.page.pageIndex = val
      this.userList()
    },
    // 4.查看详情
    detailsFn(row) {
      this.userForm = row
      this.dialogUser = true
    },
    closeDialog() {
      this.userForm = {
        userName: '', // 用户名
        roleId: '', // 角色id
        mobile: '', // 手机号
        regionId: '', // 所属区域id
        regionName: '', // 所属区域名
        status: '', // 启用状态
        image: '' // 图片
      }
      this.dialogUser = false
    },
    async opneDialog() {
      const { data } = await roleList()
      this.roleIdList = data
      const { data: data1 } = await Regionsearch(this.pageList)
      this.regionIdList = data1.currentPageRecords
    },
    userConfig() {
      this.$refs.userRef.validate(async(isOk) => {
        if (!isOk) return
        if (this.userForm.id) {
          await editroleList(this.userForm)
        } else {
          await addroleList(this.userForm)
        }
        this.userList()
        this.dialogUser = false
      })
    },
    // 图片校验
    beforeAvatarUpload(file) {
      const fileList = ['image/jpeg', 'image/png']
      const isType = fileList.includes(file.type)
      const isLt100kb = file.size / 1024 < 100
      if (!isType) {
        this.$message.error('上传头像图片只能是 PNG 格式!')
      }
      if (!isLt100kb) {
        this.$message.error('上传头像图片大小不能超过 100kB!')
      }
      return isType && isLt100kb
    },
    upload(file) {
      console.log(file)
      this.userForm.image = `http://likede2-java.itheima.net/image/${file.file.name}`
    },
    // 负责区域下拉框数据处理
    changeRegion(val) {
      const [id, name] = val.split('|')
      this.userForm.regionId = id
      this.userForm.regionName = name
    }
  }
}
</script>

<style lang="scss">
.el-dialog .el-input .el-input__inner {
  height: 40px !important;
}
</style>
